var AdminStudents = {
		
		from_id : 'form_els',
		
		form_popup_el: null,
		form_el: null,
		
		init: function() {
			this.form_el = $('.'+this.from_id);
			
			this.form_el.submit(function() {
				return AdminSubject.onFormSubmit(this);
			});
		},
	
		editSubject: function(json_data) {
			var data = data = eval("("+json_data+")");
			$('.form_els').each(function(index) {
				$(this).val(data[$(this).attr('name')]);
			});
			$('#popup_header').text('Edit Product');
			AdminPage.showForm();
		},
		
		addSubject: function() {
			$('.form_els').each(function(index) {
				$(this).val('');
			});
			$('#popup_header').text('Add Product');
			AdminPage.showForm();
		},
		onFormSubmit: function() {
			var data = {};
			$('.form_els').each(function(index) {
				data['f['+$(this).attr('name')+']'] = $(this).val();
			});
			data.cmd = 'store';
			
			MainPage.showMask('body');
			
			$.ajax({
				url: '/admin/subjects/store',
				type: 'POST',
				data: data,
				success: function(res, d, r) {
					MainPage.hideMask('body');
					
					var result = eval("(" +res+")");
					console.log(res);
					if (result.success == true) {
						document.location.href = '/admin/subjects/';
					} else {
						alert(result.errors.join("\n\r"));
					}
					
				},
				error: function(res, f, r) {
					MainPage.hideMask('body');
					alert('Some Errors Occured, please try later.');
				}
			});
			
			return false;
		}
};
$(document).ready(function(){
	AdminSubject.init();
	$('#add_b').click(function(){
		AdminSubject.addSubject();
	});
	$('.sub_edit').each(function(){
		$(this).click(function(){
			AdminSubject.editSubject($(this).attr('json'));
		});
	});
});